package com.lyf.boot.modules.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.lyf.boot.annotation.Log;
import com.lyf.boot.core.model.IdArrDto;
import com.lyf.boot.core.model.IdDto;
import com.lyf.boot.core.model.PageInfo;
import com.lyf.boot.core.model.R;
import com.lyf.boot.enums.BusinessType;
import com.lyf.boot.modules.system.entity.dto.SysConfigDto;
import com.lyf.boot.modules.system.entity.dto.SysConfigPageDto;
import com.lyf.boot.modules.system.entity.vo.SysConfigVo;
import com.lyf.boot.modules.system.service.ISysConfigService;
import com.lyf.boot.validate.group.AddGroup;
import com.lyf.boot.validate.group.EditGroup;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统配置
 * @author Luyufan
 * @date 2023/8/15 16:35
 */
@RestController
@RequestMapping("/sysConfig")
@RequiredArgsConstructor
public class SysConfigController {

    private final ISysConfigService sysConfigService;

    /**
     * 分页查询系统配置
     * @param dto 请求参数
     * @return 分页数据
     */
    @SaCheckPermission("sys:sysConfig:list")
    @PostMapping("/getSysConfigPage")
    public R<PageInfo<SysConfigVo>> getSysConfigPage(@RequestBody @Valid SysConfigPageDto dto){
        return R.ok(sysConfigService.getSysConfigPage(dto));
    }

    /**
     * 查询配置详情
     * @param dto 请求参数
     * @return 详情数据
     */
    @PostMapping("/getSysConfigInfo")
    public R<SysConfigVo> getSysConfigInfo(@RequestBody @Valid IdDto dto){
        return R.ok(sysConfigService.getSysConfigInfo(dto.getId()));
    }

    /**
     * 添加系统配置
     * @param dto 请求参数
     * @return 保存结果
     */
    @SaCheckPermission("sys:sysConfig:save")
    @PostMapping("/saveSysConfig")
    @Log(module = "参数配置", business = "添加参数配置", businessType = BusinessType.INSERT)
    public R<Void> saveSysConfig(@RequestBody @Validated(AddGroup.class)SysConfigDto dto){
        sysConfigService.saveSysConfig(dto);
        return R.ok();
    }

    /**
     * 修改系统配置
     * @param dto 请求参数
     * @return 修改结果
     */
    @SaCheckPermission("sys:sysConfig:update")
    @PostMapping("/updateSysConfig")
    @Log(module = "参数配置", business = "修改参数配置", businessType = BusinessType.UPDATE)
    public R<Void> updateSysConfig(@RequestBody @Validated(EditGroup.class)SysConfigDto dto){
        sysConfigService.updateSysConfig(dto);
        return R.ok();
    }

    /**
     * 删除系统配置
     * @param dto 请求参数
     * @return 删除结果
     */
    @SaCheckPermission("sys:sysConfig:delete")
    @PostMapping("/deleteSysConfig")
    @Log(module = "参数配置", business = "删除参数配置", businessType = BusinessType.DELETE)
    public R<Void> deleteSysConfig(@RequestBody @Valid IdArrDto dto){
        sysConfigService.deleteSysConfig(dto.getIds());
        return R.ok();
    }

}
